//
//  YDCharacteristicViewController.m
//  IngCard
//
//  Created by kf on 2017/5/31.
//  Copyright © 2017年 IngDan. All rights reserved.
//

#import "YDCharacteristicViewController.h"
#import "BabyBluetooth.h"

#define channelOnCharacteristicView @"CharacteristicView"
@interface YDCharacteristicViewController ()
@property(nonatomic, strong) BabyBluetooth *baby;
@property(nonatomic, strong) QMUITips *tips;
@end

@implementation YDCharacteristicViewController

-(void) initDataSource
{
    [super initDataSource];
    
    [self initBabyDelegate];

    
    self.baby.channel(channelOnCharacteristicView).characteristicDetails(self.peripheral.peripheral,self.chararcterstic);
    
    self.tips = [QMUITips showLoading:NSStringFormat(@"读取Characteristic的详细信息：%@",self.chararcterstic.UUID)
                               inView:self.navigationController.view
                       hideAfterDelay:1];
}

#pragma mark - <UITableViewDelegate>、<UITableViewDataSource>

//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
//    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 100, 50)];
//    //    title. = UIEdgeInsetsMake(0, 20, 0, 0);
//    CBService *service = [self.peripheral.peripheral.services objectAtIndex:section];
//    title.text = [NSString stringWithFormat:@"协议：%@", service.UUID];
//    [title setTextColor:[UIColor whiteColor]];
//    [title setBackgroundColor:[UIColor darkGrayColor]];
//    return title;
//}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30.0f;
}

#pragma mark - private

-(void) read
{

}

-(void) write
{

}

-(void) initBabyDelegate
{
    //设置读取characteristics的委托
    [self.baby setBlockOnReadValueForCharacteristicAtChannel:channelOnCharacteristicView
                                                  block:^(CBPeripheral *peripheral, CBCharacteristic *characteristic, NSError *error)
    {
        NSLog(@"characteristic :%@ value is:%@",characteristic.UUID, characteristic.value);
        NSLog(@"characteristic :%@ value is:%lu",characteristic.UUID, (unsigned long)characteristic.properties);
    }];
  
    //设置写数据成功的block
    [self.baby setBlockOnDidWriteValueForCharacteristicAtChannel:channelOnCharacteristicView block:^(CBCharacteristic *characteristic, NSError *error) {
        NSLog(@"setBlockOnDidWriteValueForCharacteristicAtChannel characteristic:%@ and new value:%@",characteristic.UUID, characteristic.value);
    }];
    
    //设置通知状态改变的block
    [self.baby setBlockOnDidUpdateNotificationStateForCharacteristicAtChannel:channelOnCharacteristicView block:^(CBCharacteristic *characteristic, NSError *error) {
        NSLog(@"uid:%@,isNotifying:%@",characteristic.UUID,characteristic.isNotifying?@"on":@"off");
    }];

}

#pragma mark - lazy load
-(BabyBluetooth *) baby
{
    if (!_baby)
    {
        _baby = [BabyBluetooth shareBabyBluetooth];
        
        //扫描选项->CBCentralManagerScanOptionAllowDuplicatesKey:忽略同一个Peripheral端的多个发现事件被聚合成一个发现事件
        NSDictionary *scanForPeripheralsWithOptions = @{CBCentralManagerScanOptionAllowDuplicatesKey:@YES};
        /*连接选项->
         CBConnectPeripheralOptionNotifyOnConnectionKey :当应用挂起时，如果有一个连接成功时，如果我们想要系统为指定的peripheral显示一个提示时，就使用这个key值。
         CBConnectPeripheralOptionNotifyOnDisconnectionKey :当应用挂起时，如果连接断开时，如果我们想要系统为指定的peripheral显示一个断开连接的提示时，就使用这个key值。
         CBConnectPeripheralOptionNotifyOnNotificationKey: 当应用挂起时，使用该key值表示只要接收到给定peripheral端的通知就显示一个提
         */
        NSDictionary *connectOptions = @{CBConnectPeripheralOptionNotifyOnConnectionKey:@YES,
                                         CBConnectPeripheralOptionNotifyOnDisconnectionKey:@YES,
                                         CBConnectPeripheralOptionNotifyOnNotificationKey:@YES};
        
        [_baby setBabyOptionsAtChannel:channelOnCharacteristicView
         scanForPeripheralsWithOptions:scanForPeripheralsWithOptions
          connectPeripheralWithOptions:connectOptions
        scanForPeripheralsWithServices:nil
                  discoverWithServices:nil
           discoverWithCharacteristics:nil];
    }
    return _baby;
}

@end
